#include "rectangle.hh"
#include <iostream>
#include <cstdlib>
using namespace mln;
using namespace mln::value;
using namespace mln::morpho;


template <typename I>
bool
get_rectangle (I& ima, float error_rate)
{
  label_8 nlabels;

  mln_ch_value(I, label_8) lbl = labeling::blobs (ima, c4 (), nlabels);

  util::array<box2d> arr = labeling::compute (accu::meta::shape::bbox (), lbl, nlabels);

  for (unsigned i = 1; i <= nlabels; ++i)
    {
      unsigned sum = 0;

      mln_VAR (region, ima | arr[i]);
      mln_piter (I) p (region.domain ());

      for_all (p)
	if (ima(p) == literal::one)
	  ++sum;

      if (sum >= error_rate * arr[i].nrows () * arr[i].ncols ())
	return true;
    }
  return false;
}

int
main (int argc, char** argv)
{
  image2d<bool> ima;

  io::pbm::load (ima, argv[1]);
  return get_rectangle (ima, atof(argv[2]));
}
